var prefix = "/jzweb/jazUserOrder";
// $(function() {
//
// });

/**
 * url 传参处理函数
 */
function getUrlParams() {
	const url = window.location.href;
	const params = url.slice(url.indexOf('?') + 1).split('&');
	const result = {};
	params.forEach(function(param) {
		const keyValue = param.split('=');
		result[keyValue[0]] =keyValue[1]
	});
	return result;
}

/**
 * 详情接口
 */
function getOrder() {
	return $.ajax({
		type : "POST",
		url :  prefix + "/getInfo",
		data : {
			initiateId:getUrlParams().initiateId,
			signingId:getUrlParams().signingId,
			id: getUrlParams().id
		},
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				// console.log(data)
				// list = data.data
				// parent.layer.msg("操作成功");
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}

/**
 * 备注修改
 * @param id 用户id
 * @param remark 备注内容
 */
function updateData(id,remark) {
	return $.ajax({
		type : "POST",
		url :  prefix + "/edit",
		data : {
			id:id,
			remark:remark
		},
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				console.log(data)
				// list = data.data
				parent.layer.msg("操作成功");
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}


/**
 * 数据处理
 */
async function  createData() {
	const res = await getOrder();
	let list = JSON.parse(JSON.stringify(res?.data))
	list.period = `${list.createTime}至${list.endTime}`
	switch (list.status) {
		case '0':
			list.status = '待开始';
			break;
		case '1':
			list.status = '进行中 ';
			break;
		case '2':
			list.status = '已完成 ';
			break;
		case '3':
			list.status = '已取消 ';
			break;
	}
	return list
}

layui.use(['form'],async function() {
	const form = layui.form;
	const list = await createData()
	$('#openContractBtn').data('valueLink', list?.contract);
	form.val('orderForm', list);
	form.val('orderCreator', list);
	form.val('contractingParty', list);

	// 监听提交按钮点击事件
	form.on('submit(formSubmit)',async function() {

		const form1Data = $('#contractingParty').serializeArray().reduce(function(obj, item) {
			obj[item.name] = item.value;
			return obj;
		}, {});

		await updateData(list.id,form1Data.remark)

		const index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
		parent.layer.close(index);
		parent.load();
		return false;
	});

});

/**
 * 查看合同
 */
$('#openContractBtn').on('click', function(event) {

	var url = "https://jinzhijz.jz1x.com/jzApi/jazUserAunt/userOrderExport?orderId="+getUrlParams().id;
	var xhr = new XMLHttpRequest();
	xhr.open('GET', url, true);
	xhr.responseType = "blob"; // 返回类型blob
	xhr.onload = function (data, textStatus, request) {
		if (this.status === 200) {
			var blob = this.response// 获取返回值
			var type = xhr.getResponseHeader('Content-Type');
			blob = new Blob([blob],{type:type});
			var a = document.createElement('a');
			a.download = '合同详情.doc';
			a.href = window.URL.createObjectURL(blob);
			a.click();
		}
	};
	xhr.send();

});